package com.example.smartfridge;

import java.util.List;

import com.example.smartfridge.R;
import com.id2216.fridge.data.FridgeItem;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FridgeAdapter extends ArrayAdapter<FridgeItem> {

    Context context;
    int layoutResourceId;   
    List<FridgeItem> data = null;
    
	public FridgeAdapter(Context context, int resource, List<FridgeItem> fridgeItemList) 
	{
		super(context, resource, fridgeItemList);
		
        this.layoutResourceId = resource;
        this.context = context;
        this.data = fridgeItemList;
 	}
	
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        FridgeHolder holder = null;
       
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
           
            holder = new FridgeHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.fridgeIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtExpire = (TextView)row.findViewById(R.id.txtExpire);
           
            row.setTag(holder);
        }
        else
        {
            holder = (FridgeHolder)row.getTag();
        }
       
        FridgeItem item = data.get(position);
        holder.txtTitle.setText(item.getName());
        holder.txtExpire.setText(item.getExpireDay());
        
        // Set text style and color
        if(item.isExpire() == true)
        {	
        	holder.txtTitle.setTextColor(Color.parseColor("#800000"));
        	holder.txtTitle.setTypeface(null,Typeface.BOLD);
        	holder.txtExpire.setTextColor(Color.parseColor("#800000"));
        	holder.txtExpire.setTypeface(null,Typeface.BOLD);
        }
        else
        {
            holder.txtTitle.setTextColor(Color.BLACK);
        	holder.txtTitle.setTypeface(null,Typeface.NORMAL);
            holder.txtExpire.setTextColor(Color.BLACK);
        	holder.txtExpire.setTypeface(null,Typeface.NORMAL);
        }
        return row;
    }
	
	static class FridgeHolder
	{
		ImageView imgIcon;
		TextView txtTitle;
		TextView txtExpire;
	}

}
